package com.feng.security.mac;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Test {

	public static void main(String[] args) {
		try {
			// Want to encrypt data
			String data = /*"26AA4B687EC030F2D332B301F8F5604356885B27D4AD2ABDCE81AFC3C01D6EDD";*/"456";
			// Key
			String key = /*"BD612FDD827BF7EE992EB04AF1E342AF";*/"123";
			our(data, key);
			standard(data, key);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static void our(String data, String key) {
		byte[] b = HMACSHA1.getHmacSHA1(data, key);
		String s = new BASE64Encoder().encode(b);
		System.out.println("hmacsha1 = " + s);
	}

	private static void standard(String data, String key) {
		byte[] byteHMAC = null;
		try {
			Mac mac = Mac.getInstance("HmacSHA1");
			SecretKeySpec spec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
			mac.init(spec);
			byteHMAC = mac.doFinal(data.getBytes());
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException ignore) {
		}
		String oauth = new BASE64Encoder().encode(byteHMAC);
		System.out.println("standard = " + oauth);
	}
}
